package com.ruoyi.web.controller.client;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.constant.GysType;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.DictUtils;
import com.ruoyi.common.utils.ServletUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.web.service.TokenService;
import com.ruoyi.system.domain.client.BaseCustomerSupplier;
import com.ruoyi.system.service.IClientSupplierService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.Arrays;
import java.util.List;

/**
 * @author GE-LV5
 * @create 2023/3/14 10:20
 */
@Api(tags ="客户供应商")
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@RequestMapping("/client/supplier" )
public class ClientSupplierController extends BaseController {
     @Autowired
     IClientSupplierService iClientSupplierService;

    @Autowired
    private TokenService tokenService;


    /**
     * 查询客户供应商列表
     */
    @ApiOperation(value = "查询-客户供应商列表")
    @PostMapping("/list")
    public TableDataInfo list(@RequestBody BaseCustomerSupplier clientSupplier){
        startPage();
        LambdaQueryWrapper<BaseCustomerSupplier> lqw = new LambdaQueryWrapper<BaseCustomerSupplier>();
        if (StringUtils.isNotBlank(clientSupplier.getCode())){
            lqw.like(BaseCustomerSupplier::getCode ,clientSupplier.getCode());
        }
        if (StringUtils.isNotBlank(clientSupplier.getName())){
            lqw.eq(BaseCustomerSupplier::getName ,clientSupplier.getName());
        }
        if (StringUtils.isNotBlank(clientSupplier.getBaseDescribe())){
            lqw.eq(BaseCustomerSupplier::getBaseDescribe ,clientSupplier.getBaseDescribe());
        }
        if (StringUtils.isNotBlank(clientSupplier.getType())){
            lqw.eq(BaseCustomerSupplier::getType ,clientSupplier.getType());
        }
        List<BaseCustomerSupplier> list = iClientSupplierService.list(lqw);
        //根据
//        list.forEach(item->{
//            item.setType(DictUtils.getDictLabel(GysType.GYS_TYPE,item.getType()));
//        });
        return getDataTable(list);
    }

    /**
     * 新增客户供应商
     */
    @Log(title = "客户供应商" , businessType = BusinessType.INSERT)
    @ApiOperation(value = "新增-客户供应商")
    @PostMapping
    public AjaxResult add(@RequestBody BaseCustomerSupplier clientSupplier) {
        return toAjax(iClientSupplierService.save(clientSupplier) ? 1 : 0);
    }

    /**
     * 修改客户供应商
     */
    @Log(title = "客户供应商" , businessType = BusinessType.UPDATE)
    @ApiOperation(value = "修改-客户供应商")
    @PutMapping
    public AjaxResult edit(@RequestBody BaseCustomerSupplier clientSupplier) {
        return toAjax(iClientSupplierService.updateById(clientSupplier) ? 1 : 0);
    }

    /**
     * 删除客户供应商
     */
    @Log(title = "客户供应商" , businessType = BusinessType.DELETE)
    @ApiOperation(value = "删除-客户供应商")
    @DeleteMapping("/{teamIds}" )
    public AjaxResult remove(@PathVariable Long[] teamIds) {
        return toAjax(iClientSupplierService.removeByIds(Arrays.asList(teamIds)) ? 1 : 0);
    }

    /**
     * 导出客户供应商
     */
    @Log(title = "客户供应商", businessType = BusinessType.EXPORT)
    @ApiOperation(value = "导入客户供应商列表")
    @GetMapping("/export")
    public AjaxResult export(BaseCustomerSupplier clientSupplier) {
        LambdaQueryWrapper<BaseCustomerSupplier> lqw = new LambdaQueryWrapper<BaseCustomerSupplier>(clientSupplier);
        List<BaseCustomerSupplier> list = iClientSupplierService.list(lqw);
        ExcelUtil<BaseCustomerSupplier> util = new ExcelUtil<BaseCustomerSupplier>(BaseCustomerSupplier.class);
        return util.exportExcel(list, "导入操作步骤数据");
    }

    /**
     * 导入客户供应商列表
     */
    @Log(title = "导入客户供应商", businessType = BusinessType.IMPORT)
    @ApiOperation(value = "导入客户供应商列表")
    @PostMapping("/importDataAdd")
    public AjaxResult importDataAdd(@ApiParam(value = "导入文件", required = true) MultipartFile file, boolean updateSupport) throws Exception {
        ExcelUtil<BaseCustomerSupplier> util = new ExcelUtil<BaseCustomerSupplier>(BaseCustomerSupplier.class);
        List<BaseCustomerSupplier> areaList = util.importExcel(file.getInputStream());
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        String operName = loginUser.getUsername();
        String message = iClientSupplierService.importArea(areaList,updateSupport, operName);
        return AjaxResult.success(message);
    }

    /**
     * 导入模板
     */
    @ApiOperation(value = "导入模板")
    @GetMapping("/importTemplate")
    public AjaxResult importTemplate()
    {
        List<BaseCustomerSupplier> list = iClientSupplierService.selectListOrdersFordTempList(new BaseCustomerSupplier());
        ExcelUtil<BaseCustomerSupplier> util = new ExcelUtil<BaseCustomerSupplier>(BaseCustomerSupplier.class);
        return util.importTemplateExcel(list,"客户订单数据");
    }


}
